//
//  WZCheckController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/7/5.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZCheckController.h"
#import "WZDetailTask.h"
#import "WZRefuseViewController.h"
#import "WZTaskLogController.h"

#define TaskManager_Assigner_Authorized_Refused @""
#define TaskManager_Assigner_Authorized_Agreed  @"您已同意"
#define TaskManager_Assigner_Authorized_        @""


typedef enum : NSUInteger {
    WZCheckControllerRefuseBtnTag = 6000,
    WZCheckControllerAgreeBtnTag,
} WZCheckControllerBtnTag;

@interface WZCheckController ()

@property (weak, nonatomic) IBOutlet UILabel *taskName;
@property (weak, nonatomic) IBOutlet UILabel *desc;
@property (weak, nonatomic) IBOutlet UILabel *beginTime;
@property (weak, nonatomic) IBOutlet UILabel *endTime;
@property (weak, nonatomic) IBOutlet UILabel *projectName;
@property (weak, nonatomic) IBOutlet UILabel *statusName;
@property (weak, nonatomic) IBOutlet UILabel *progress;
@property (weak, nonatomic) IBOutlet UILabel *exturoName;
@property (weak, nonatomic) IBOutlet UILabel *assignerName;

@property (nonatomic, weak) UIButton *refuseBtn;
@property (nonatomic, weak) UIButton *agreeBtn;
@property (nonatomic, copy) NSString *refuseReason;
@property (nonatomic, copy) NSString *extApplyId;

@property (nonatomic, strong) WZDetailTask *detailTask;

@end

@implementation WZCheckController

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self getTaskDetailInfor];
    
    [self assignerFetchSchedule];
    
}


- (void)setupTableView {
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.tableView.tableFooterView.backgroundColor = [UIColor greenColor];
    
    UIButton *refuseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.refuseBtn = refuseBtn;
    [refuseBtn setTitle:@"拒绝" forState:UIControlStateNormal];
    refuseBtn.backgroundColor = WZRGBColor(247, 187, 66);
    refuseBtn.frame = CGRectMake(0, HARDWARE_SCREEN_HEIGHT - 44 - 64, HARDWARE_SCREEN_WIDTH * 0.5, 44);
    [refuseBtn addTarget:self action:@selector(checkVCBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:refuseBtn];
    refuseBtn.tag = WZCheckControllerRefuseBtnTag;
    
    
    UIButton *agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.agreeBtn = agreeBtn;
    [agreeBtn setTitle:@"同意" forState:UIControlStateNormal];
    agreeBtn.backgroundColor = WZRGBColor(94, 196, 206);
    agreeBtn.frame = CGRectMake(HARDWARE_SCREEN_WIDTH - refuseBtn.width, HARDWARE_SCREEN_HEIGHT - 44 - 64, HARDWARE_SCREEN_WIDTH * 0.5, 44);
    [agreeBtn addTarget:self action:@selector(checkVCBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:agreeBtn];
    agreeBtn.tag = WZCheckControllerAgreeBtnTag;
    self.tableView.backgroundColor = WZRGBColor(245, 246, 249);
    self.title = @"任务详情";
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.scrollEnabled = NO;
}


/**任务详细信息*/
- (void)getTaskDetailInfor {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"name"] = self.oaUserName;
    params[@"pwd"] = self.oaPwd;
    params[@"taskId"] = self.taskId;
    
    [NetWorkShare getTaskDetailInforWithParams:params SuccessHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                self.detailTask = [WZDetailTask objectWithKeyValues:responseObject[@"detail"]];
                
                [self loadData];
                
                // 知会我的
                if (![self.oaUserId isEqualToString:self.detailTask.executorId] && ![self.oaUserId isEqualToString:self.detailTask.assignerId]) {
                    
                    self.refuseBtn.hidden = YES;
                    self.agreeBtn.hidden = YES;
                    
                    // 我是分配人不是执行者
                } else if ([self.oaUserId isEqualToString:self.detailTask.assignerId] && ![self.oaUserId isEqualToString:self.detailTask.executorId]) {
                    
                    self.refuseBtn.hidden = NO;
                    self.agreeBtn.hidden = NO;
                    
                    // 我是执行者不是分配者
                } else if (![self.oaUserId isEqualToString:self.detailTask.assignerId] && [self.oaUserId isEqualToString:self.detailTask.executorId]) {
                    
                    self.refuseBtn.hidden = YES;
                    self.agreeBtn.hidden = YES;
                    
                    // 执行和分配是同一个人
                } else if ([self.oaUserId isEqualToString:self.detailTask.assignerId] && [self.oaUserId isEqualToString:self.detailTask.executorId]) {
                    
                    self.refuseBtn.hidden = NO;
                    self.agreeBtn.hidden = NO;
                }
                
                [self.tableView reloadData];
                
                WZLog(@"%@", responseObject);
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
        } else {
            
            [MBProgressHUD showError:UNKNOWNERROR];
        }
    } FailureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showError:NETWORKNERROR];
        WZLog(@"error = %@", error);
    }];
    
}

- (void)loadData {
    
    self.taskName.text = self.detailTask.taskName;
    self.desc.text = self.detailTask.remark;
    self.beginTime.text = self.detailTask.planExecBeginDate;
    self.endTime.text = self.detailTask.planExecEndDate;
    self.projectName.text = self.detailTask.projectName;
    self.exturoName.text = self.detailTask.executorName;
    self.assignerName.text = self.detailTask.assignerName;
    self.statusName.text = self.detailTask.statusName;
    self.progress.text = [NSString stringWithFormat:@"%@%%", self.detailTask.progress];
}


- (void)assignerFetchSchedule {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"name"] = self.oaUserName;
    params[@"pwd"] = self.oaPwd;
    params[@"taskId"] = self.taskId;
    
    [NetWorkShare assignerFetchScheduleWithParams:params SuccessHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if ([[responseObject objectForKey:@"success"] boolValue]) {
            
            self.extApplyId = responseObject[@"detail"][@"id"];
//            self.extApplyId = responseObject[];
            WZLog(@"response = %@", responseObject);
            
        } else {
            
            [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
        }
        
    } FailureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NETWORKNERROR];
    }];
}


- (void)checkVCBtnDidClick:(UIButton *)sender {
    
    switch (sender.tag) {
        case WZCheckControllerRefuseBtnTag:
            
        {
            WZRefuseViewController *refuseVC = [[WZRefuseViewController alloc] init];
            refuseVC.oaUserId = self.oaUserId;
            refuseVC.oaUserName = self.oaUserName;
            refuseVC.taskId = self.taskId;
            refuseVC.oaPwd = self.oaPwd;
            refuseVC.oaCompanyID = self.oaCompanyID;
            refuseVC.assignerId = self.assignerId;
            refuseVC.executorId = self.executorId;
            
            [self.navigationController pushViewController:refuseVC animated:YES];
        }
            
            break;
            
        case WZCheckControllerAgreeBtnTag:
            
        {
            if (!self.extApplyId) {
                
                [MBProgressHUD showError:@"获取待办信息错误"];
                return;
            }
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            params[@"name"] = self.oaUserName;
            params[@"pwd"] = self.oaPwd;
            params[@"extApplyId"] = self.extApplyId;
            params[@"result"] = @"1"; // 同意
            params[@"refuseReason"] = @"";
            
            [NetWorkShare assignerAuthorizedWithParams:params SuccessHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
                
                if ([[responseObject objectForKey:@"success"] boolValue]) {
                    
                    [MBProgressHUD showSuccess:TaskManager_Assigner_Authorized_Agreed];
                    self.agreeBtn.hidden = YES;
                    self.refuseBtn.hidden = YES;
                    [self.tableView reloadData];
                } else {
                    
                    [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
                }
                
            } FailureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:NETWORKNERROR toView:self.view];
            }];
 
        }
            
            break;
            
        default:
            break;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (6 == indexPath.row) {
        
        WZTaskLogController *logVc = [[WZTaskLogController alloc] init];
        logVc.taskId = self.taskId;
        logVc.oaUserName = self.oaUserName;
        logVc.oaPwd = self.oaPwd;
        [self.navigationController pushViewController:logVc animated:YES];
    }
}

@end
